import { api } from "./src/variable";

import { invoke, notify } from "./src/native-to-h5";

import { alias, aliasBridge, wrap, invokeApp } from "./src/adapter";

import { setDebug } from "./src/debug";

import analyicScript from "./src/script-analyic";

import ACMD from "./src/acmd";

import "./src/action/index";

ACMD(api);

// 对外定义的API
api.invoke = invoke;
api.notify = notify;
api.debug = setDebug;

// 对外输出此方法,用于特定项目去开发各自应用的专属交互操作
api.wrap = action =>
	wrap(action, (kv, callback) => {
		invokeApp(action, kv, callback);
		return api;
	});

// 注册事件action，主要提供App端简化
api.on = function (action, handler) {
	wrap("on." + action, handler);
	return api;
};

window.JSApi = api;

const query = analyicScript();

if (query && query.length) {
	query[0] !== "_" && query[0] !== "JSApi" && alias.call(api, query[0]);

	if (query.length === 2) {
		aliasBridge.call(api, query[1]);
	}
}
